package switchviz.core.signal;

import switchviz.core.SwitchVizException;
import switchviz.core.data.IData;


public class Signal implements ISignal
{
	private String _aliasName = "";
	private int _port;
	
	private ISignalDataModel _signalDataModel = null;
	private IData _data = null;

	/* c'tors ****************************************************************/
	public Signal()
	{ }
	
	public Signal(String aliasName)
	{
		this(aliasName, 0);
	}
	
	public Signal(String aliasName, int port)
	{
		setAliasName(aliasName);
		setPort(port);
	}

	/* events ****************************************************************/
	protected void dataChanged()
	{
		releaseDataModel();
	}
	
	/* properties ************************************************************/
	public String getAliasName()
	{
		return _aliasName;
	}
	
	public void setAliasName(String aliasName)
	{
		_aliasName = aliasName;
		
		releaseDataModel();
	}
	
	public int getPort()
	{
		return _port;
	}
	
	public void setPort(int port)
	{
		_port = port;
	}
	
	public IData getData()
	{
		return _data;
	}
	
	public void setData(IData data)
	{
		_data = data;
		
		dataChanged();
	}

	/* methods ***************************************************************/
	public boolean isPortRequired() throws SwitchVizException
	{
		return getDataModel().isPortRequired();
	}
	
	public String getOid() throws SwitchVizException
	{
		if (isPortRequired())
			return getDataModel().getOid() + "." + _port;
		
		return getDataModel().getOid();
	}
	
	protected ISignalDataModel getDataModel() throws SwitchVizException
	{
		if (_signalDataModel == null)
			_signalDataModel = new SignalDataModel(getAliasName());
		
		return _signalDataModel;
	}
	
	protected void releaseDataModel()
	{
		_signalDataModel = null;
	}
}
